/**
 * 
 */
package com.jytc.framework.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.jytc.framework.bo.FwLoginBO;
import com.jytc.framework.commons.ResultInfos;
import com.jytc.framework.constants.RequestNameConstants;
import com.jytc.framework.constants.ResultConstants;
import com.jytc.framework.constants.SessionNameConstants;
import com.jytc.framework.constants.UrlConstants;
import com.jytc.framework.utils.SessionUtils;

/**
 * @title: 
 * @description: 
 * 
 * @author: dongyuese
 *
 */
@Controller
@RequestMapping("/admin/jsp/sysLogin.do")
public class FwLoginController
{

	private static final String LOGIN_PAGE = "/admin/jsp/login.jsp"; //登录页面
	private static final String MAIN_PAGE = "/admin/jsp/main.jsp"; //主框架页面

	protected Logger logger = LoggerFactory.getLogger(getClass());//日志
	@Autowired
	private FwLoginBO fwLoginBO;

	/**
	 * 用户登录
	 * 
	 * @param request
	 * @param loginName
	 * @param loginPwd
	 * @param checkCode
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(params = "method=login")
	public ModelAndView login(HttpServletRequest request, String loginName, String loginPwd, String checkCode, ModelMap model)
			throws Exception
	{
		ResultInfos resultInfos = new ResultInfos();
		model.addAttribute(RequestNameConstants.RESULT_INFO, resultInfos);

		try
		{
			/**
			 * 调用登录业务逻辑
			 */
			int loginResultCode = fwLoginBO.login(loginName, loginPwd, checkCode, request);

			if (loginResultCode == ResultConstants.LOGIN_SUCCESS)
			{
				return new ModelAndView(MAIN_PAGE);
			} else
			{
				resultInfos.setPromptInfo(ResultConstants.getResultInfo(loginResultCode));
				resultInfos.setIsBack(false);
				resultInfos.setIsRedirect(true);
				resultInfos.setGotoUrlForward(LOGIN_PAGE);
			}
		} catch (Exception e)
		{
			logger.error("用户登录异常", e);

			// 显示提示信息
			resultInfos.setPromptInfo(ResultConstants.getResultInfo(ResultConstants.LOGIN_ERROR));
			resultInfos.setIsBack(true);
		}
		return new ModelAndView(UrlConstants.INFORMATION_PAGE);
	}

	/**
	 * 用户登出
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "method=loginOut")
	public ModelAndView login(HttpServletRequest request)
	{

		SessionUtils.removeObjectAttribute(request, SessionNameConstants.ADMIN_LOGIN_TOKEN);
		return new ModelAndView(LOGIN_PAGE);
	}

}
